
const dotenv = require('dotenv');
// 从 .env 文件中读取配置项
dotenv.config();
var os = require('os');
//详细配置https://blog.csdn.net/liuxingyuzaixian/article/details/100976199/
var config = {};
config.development = {
  // 数据库信息
  db: {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    database: process.env.DB_NAME,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    dialect: process.env.DB_TYPE,
    logging: false,
    operatorsAliases: false,
  },
  jwt: {
    // Recommended: 63 随机数 random alpha-numeric characters
    // 可以使用该网站直接生成: https://www.grc.com/passwords.htm
    tokenSecret: process.env.TOKEN_SECRET
  },
  //总配置
  common: {
    /*
     * tryLoginTimes is control login error times to avoid force attack.
     * if value is 0, no limit for login auth, it may not safe for account. when it's a number, it means you can
     * try that times today. but it need config redis server.
     */
    tryLoginTimes: 0,
    // CodePush Web(https://github.com/lisong/code-push-web) login address.
    //codePushWebUrl: "http://127.0.0.1:3001/login",
    // create patch updates's number. default value is 3
    diffNums: 3,
    // 图片等资源文件与jsbundle
    dataDir: process.env.DATA_DIR,
    // 存储类型，加入配置了oss，这里填写oss ("local" | "qiniu" | "s3"| "oss" || "tencentcloud")
    storageType: process.env.STORAGE_TYPE,
    // options value is (true | false), when it's true, it will cache updateCheck results in redis.
    updateCheckCache: false,
    // options value is (true | false), when it's true, it will cache rollout results in redis
    rolloutClientUniqueIdCache: false,
  },
  // 不使用云存储，直接使用服务器存储
  local: {
    // 数据库相关的资料路径记录
    storageDir: process.env.STORAGE_DIR,
    // （本机ip地址） Binary files download host address which Code Push Server listen to. the files storage in storageDir.
    downloadUrl: process.env.LOCAL_DOWNLOAD_URL,
    // public static download spacename.
    public: process.env.LOCAL_PUBLIC_URL
  },
  // 阿里云 OSS (https://www.aliyun.com/product/oss) when storageType value is "oss".
  oss: {
    accessKeyId: process.env.ALI_OSS_ACCESS_KEY_ID,
    secretAccessKey: process.env.ALI_OSS_SECRET_ACCESS_KEY,
    endpoint: process.env.ENDPOINT,
    bucketName: process.env.BUCKET_NAME,
    prefix: process.env.PREFIX, // Key prefix in object key
    downloadUrl: process.env.DOWNLOAD_URL, // binary files download host address.
  },
  // 腾讯云 COS (https://cloud.tencent.com/product/cos) when storageType value is "oss".
  tencentcloud: {
    accessKeyId: "",
    secretAccessKey: "",
    bucketName: "",
    region: "",
    downloadUrl: "", // binary files download host address.
  },
  // 七牛云 (http://www.qiniu.com/) 
  qiniu: {
    accessKey: "",
    secretKey: "",
    bucketName: "",
    downloadUrl: "" // Binary files download host address.
  },
  // 又拍云 (https://www.upyun.com/) storage when storageType value is "upyun"
  upyun: {
    storageDir: process.env.UPYUN_STORAGE_DIR,
    serviceName: process.env.UPYUN_SERVICE_NAME,
    operatorName: process.env.UPYUN_OPERATOR_NAME,
    operatorPass: process.env.UPYUN_OPERATOR_PASS,
    downloadUrl: process.env.DOWNLOAD_URL,
  },
  // 国外的aws Config for Amazon s3 (https://aws.amazon.com/cn/s3/) storage when storageType value is "s3".
  s3: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    sessionToken: process.env.AWS_SESSION_TOKEN, //(optional)
    bucketName: process.env.BUCKET_NAME,
    region: process.env.REGION,
    downloadUrl: process.env.DOWNLOAD_URL, // binary files download host address.
  },
  // Config for smtp email，register module need validate user email project source https://github.com/nodemailer/nodemailer
  //配置smtp电子邮件，注册模块需要验证用户电子邮件项目源
  smtpConfig: {
    host: "smtp.aliyun.com",
    port: 465,
    secure: true,
    auth: {
      user: "",
      pass: ""
    }
  },
  // Config for redis (register module, tryLoginTimes module)
  redis: {
    default: {
      host: "127.0.0.1",
      port: 6379,
      retry_strategy: function (options) {
        if (options.error.code === 'ECONNREFUSED') {
          // End reconnecting on a specific error and flush all commands with a individual error
          return new Error('The server refused the connection');
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
          // End reconnecting after a specific timeout and flush all commands with a individual error
          return new Error('Retry time exhausted');
        }
        if (options.times_connected > 10) {
          // End reconnecting with built in error
          return undefined;
        }
        // reconnect after
        return Math.max(options.attempt * 100, 3000);
      }
    }
  }
}

config.development.log4js = {
  appenders: { console: { type: 'console' } },
  categories: {
    "default": { appenders: ['console'], level: 'error' },
    "startup": { appenders: ['console'], level: 'info' },
    "http": { appenders: ['console'], level: 'info' }
  }
}

config.production = Object.assign({}, config.development);
module.exports = config;
